﻿using System;
using System.Text;
using ClearSpringNet.Stream.Cardinality;

namespace CardinalityTest
{
    class Program
    {
        static void Main(string[] args)
        {
            HlogLogTest();

            //LogLogTestB();
            //LogLogTest();
            //StringIntRepresentation();
            //var a = 8;
            //Console.WriteLine(Integer.NumberOfTrailingZeros(a));
            //Console.WriteLine(Integer.NumberOfLeadingZeros(a));
        }

        private static void HlogLogTest()
        {
            var logLog = new HyperLogLog(0.065);
            var words = new[] { "aardvark", "aardvark", "aardvark", "abyssinian", "zoology", "aardvark" }; // 2336 words
            var seed = Math.Floor((double)logLog.MaxPower); // make more fun

            for (var i = 0; i < words.Length; ++i)
            {
                var key = HyperLogLog.fnv1a(words[i]) ^ (uint)seed;
                logLog.StreamInKey((int)key);
            }

            var res = logLog.GetItemCount();

            var searchkey = "aardvark1";
            var searchState = logLog.GetKey(searchkey);

            Console.WriteLine("search state:" + searchState);
            Console.WriteLine(res);
        }

        //private static void LogLogTestB()
        //{
        //    const string streamBytes = "blah blah blah";
        //    var data = Encoding.Default.GetBytes(streamBytes);
        //    var x = new AdaptiveCounting(data);
        //    Console.WriteLine(x.Cardinality());
        //}

        //private static void LogLogTest()
        //{
        //    var x = new AdaptiveCounting(2);


        //    string a = "ABC";
        //    string b = "EFC";
        //    string c = "ABF";
        //    string d = "ABF";
        //    string e = "XYC";

        //    x.Offer(a);
        //    x.Offer(b);
        //    x.Offer(c);
        //    x.Offer(d);
        //    x.Offer(e);

        //    var s = x.SizeOf();
        //    var resultBytes = x.GetBytes();
        //    Console.WriteLine("Size are:" + s);
        //    var data = Encoding.Default.GetString(resultBytes);
        //    Console.WriteLine(data);
        //    Console.WriteLine("Number of unique element : " + x.Cardinality());

        //}

        private static void StringIntRepresentation()
        {
            var number = 8;
            var i = 0;
            var pos = 31;

            while (i < 32)
            {
                if ((number & (1 << i)) != 0)
                {
                    // b[pos] = '1';
                    Console.Write("1");
                }
                else
                {
                    Console.Write("0");
                    //b[pos] = '0';
                }

                pos--;
                i++;
            }
        }
    }
}

